package org.light.jpa.verb;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.exception.ValidateException;
import org.light.jpa.core.JpqlReflector;
import org.light.utils.InterVarUtil;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class Activate extends org.light.verb.Activate {

	@Override
	public Method generateDaoImplMethod() throws Exception{
		return null;
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception{
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("activate"+StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("void"));
			method.setThrowException(true);
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getClassType()));
			method.addMetaData("Transactional");
			method.addMetaData("Modifying");
			method.addMetaData("Query(\""+JpqlReflector.generateActivateJpqlWithValue(domain)+"\")");
			return method;
		}
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception{
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("activate"+StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("Boolean"));
			method.setThrowException(true);
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getClassType()));
			return method;
		}
	}

	@Override
	public Method generateServiceImplMethod() throws Exception{
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("activate"+StringUtil.capFirst(this.domain.getStandardName()));
			method.setReturnType(new Type("Boolean"));
			method.setThrowException(true);
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDaoSuffix()+"."+this.domain.getStandardName()+"Dao");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
			method.addSignature(new Signature(1,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getClassType()));
			method.addMetaData("Override");
		
			//Service method
			Method daomethod = this.generateDaoMethodDefinition();
					
			List<Writeable> list = new ArrayList<Writeable>();
			list.add(new Statement(100L,2, daomethod.generateStandardDaoImplCallString(InterVarUtil.DB.dao.getVarName())+";"));
			list.add(new Statement(2000L,2,"return true;"));
			method.setMethodStatementList(WriteableUtil.merge(list));
			return method;
		}
	}

	public Activate(Domain domain) throws ValidateException{
		super(domain);
	}
}
